package com.zsule.service.impl;

import java.util.List;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.zsule.entity.EUser;
import com.zsule.frameworks.mongodb.service.MongoAbstractService;
import com.zsule.service.IUserService;
import com.zsule.utils.security.MD5;

@Service("euserService")
public class EUserServiceImpl extends MongoAbstractService<EUser> implements IUserService{


	@Override
	public EUser getByUserName(EUser euser) {
		return findOne(Criteria.where("username").is(euser.getUsername()));
	}
	
	@Override
	public EUser getByEmail(String email) {
		return findOne(Criteria.where("email").is(email));
	}
	
	@Override
	public EUser getByEmail(EUser euser) {
		return getByEmail(euser.getEmail());
	}

	@Override
	public EUser getByMobile(EUser euser) {
		return getByMobile(euser.getMobile());
	}
	
	@Override
	public EUser getByMobile(String mobile) {
		return findOne(Criteria.where("mobile").is(mobile));
	}

	@Override
	public List<EUser> getUserByObj(EUser euser) {
		return findList(Criteria.where("email").is(euser.getEmail()).and("username").is(euser.getUsername()));
	}

	@Override
	public void updateEUserShopper(EUser euser) {
		updateFirst(new Query(Criteria.where("id").in(euser.getId())), Update.update("isShopper", euser.getIsShopper()).set("shopName", euser.getShopName()));
	}

	@Override
	public EUser getByMobileAndPwd(EUser euser) {
		if(euser==null){
			return null;
		}
		return getByMobileAndPwd(euser.getMobile(),euser.getPassword());
	}

	@Override
	public EUser getByMobileAndPwd(String mobile,String pwd) {
		return findOne(Criteria.where("mobile").is(mobile).and("password").is(MD5.getMD5(pwd)));
	}

	@Override
	public void bindEmail(String userid,String email) {
		updateFirst(new Query(Criteria.where("id").is(userid)), Update.update("email", email));
		
	}
}
